package com.hdoit.kylin.web.document.providers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import org.apache.poi.util.StringUtil;
import org.springframework.util.StringUtils;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item;
import com.hdoit.kylin.logical.document.model.ReportCataLogEmployee;
import com.hdoit.kylin.logical.document.service.IReportCataLogEmployeeService;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.web.common.CommonWebUtils;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.message.ListItem;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.PageContextUtils;
import com.tennetcn.component.attribute.provider.IMultipleOptionListProvider;
import com.tennetcn.plugins.devshorts.enumerable.Enumerable;

public class ReportCataLogEmployeeProvider implements IMultipleOptionListProvider<ListItem>{

	
	private List<ListItem> dataList;
	
	public ITeacherService getTeacherService() {
		return CommonApplicationContextUtil.getCurrentContext().getBean(ITeacherService.class);
	}



	public IStudentService getStudentService() {
		return CommonApplicationContextUtil.getCurrentContext().getBean(IStudentService.class);
	}



	@Override
	public List<ListItem> getDataList(Object viewData) {
		PageContext pageContext = (PageContext)viewData;
		String key = PageContextUtils.getParam(pageContext, "key");
		
		//应该限定在group内部？
		List<Student> students = getStudentService().queryList();
		List<Teacher> teachers = getTeacherService().queryList();
		
		dataList = new ArrayList<ListItem>();
		for(Student student:students){
			ListItem listItem = new ListItem();
			listItem.setText(student.getStudentName());
			listItem.setValue(student.getEmployeeId());
			dataList.add(listItem);
		}
		for(Teacher teacher:teachers){
			ListItem listItem = new ListItem();
			listItem.setText(teacher.getTeacherName());
			listItem.setValue(teacher.getEmployeeId());
			dataList.add(listItem);
		}
		
		return dataList;
	}

	@Override
	public List<ListItem> getSelectedItems(Object value, Object viewData) {
		if(dataList == null || dataList.size() <=0){
			return null;
		}
		
		if(StringUtils.isEmpty(value)){
			PageContext pageContext = (PageContext)viewData;
			String reportId = PageContextUtils.getParam(pageContext, "key");
			if(reportId.isEmpty()){
				return null;
			}
			List<String> employeeIds = getEmployeeIdsByReportId(reportId);
			
			if(employeeIds==null||employeeIds.size()<=0){
				return null;
			}
			
			List<ListItem> selDataList = new ArrayList<ListItem>();
			for(String employeeId:employeeIds){
				ListItem listItem = Enumerable.init(dataList).filter(data->employeeId.equals(data.getValue())).first();
				
				if(listItem != null){
					selDataList.add(listItem);
				}
			}
			return selDataList;
		}
		return null;
	}



	private List<String> getEmployeeIdsByReportId(String reportId) {
		IReportCataLogEmployeeService reportCataLogEmployeeService = CommonApplicationContextUtil.getCurrentContext().getBean(IReportCataLogEmployeeService.class);
		List<ReportCataLogEmployee> reportCataLogEmployees = reportCataLogEmployeeService.queryListByReportCataLog(reportId);
		List<String> employeeIds = new ArrayList<String>();
		for(ReportCataLogEmployee temp:reportCataLogEmployees){
			employeeIds.add(temp.getEmployeeId());
		}
		return employeeIds;
	}

}
